import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.ArrayList;

public class LeagueScreen {
	// Create a new JFrame with a title of "Coverage Corner"

	JFrame leagueFrame = new JFrame("Coverage Corner - League Screen");
	RecentScore[] recentScores;
	MatchUp[] matchUPS;
	League league;
	Team TeamA;
	Team TeamB;
	JTable table;
	RecentScoresScreen rSS = new RecentScoresScreen();
	LiveVideoScreen LVS = new LiveVideoScreen();
	MatchUpScreen mUS = new MatchUpScreen();
	String data[][] = new String[10][12];
	String col[] = new String[12];
	DefaultTableModel model = new DefaultTableModel(data, col);
	
	JList list = new JList();
	JTextArea textArea = new JTextArea(5, 20);

	public LeagueScreen() {

	}

	public void runLeagueScreen(League L, MatchUp[] matchup,
			RecentScore[] recent) {

		Toolkit tk = Toolkit.getDefaultToolkit();
		int screenWidth = ((int) tk.getScreenSize().getWidth());
		int screenHeight = ((int) tk.getScreenSize().getHeight());

		this.league = L;
		this.matchUPS = matchup;
		this.recentScores = recent;

		// Set background color to black
		Container con = leagueFrame.getContentPane();
		con.setBackground(Color.BLACK);
		// Make sure sizing is alright
		leagueFrame.validate();

		JPanel bigPanel = new JPanel();
		bigPanel.setBackground(Color.black);
		bigPanel.setLayout(null);
		bigPanel.setBounds(0, 0, screenWidth, screenHeight);

		JPanel leaguePanel = new JPanel();
		leaguePanel.setBackground(Color.darkGray);
		// Create a new vertical box layout, that way when we add the top five,
		// it will go top to bottom
		leaguePanel.setLayout(new BoxLayout(leaguePanel, BoxLayout.Y_AXIS));
		// Set the box to the top-left corner of the screen, then top and then
		// left
		leaguePanel.setBounds(0, screenHeight / 6, screenWidth / 3,
				screenHeight / 2);
		leaguePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		leaguePanel.setAlignmentY(Component.TOP_ALIGNMENT);

		/*
		 * // This is where we start adding the JLabels to each of the panels.
		 * First thing is the rank,score,streak, and moves default text JLabel
		 * teamMemberInfo = new JLabel(
		 * " Rank              Total Points              Streak                Moves"
		 * ); //This sets the font to a bigger size, since it's fairly small to
		 * begin with. Font newF = new
		 * Font(teamMemberInfo.getFont().getName(),teamMemberInfo
		 * .getFont().getStyle(),(int)leaguePanel.getWidth()/2/20); // This is
		 * what actually sets the text for the JLabel
		 * teamMemberInfo.setFont(newF);
		 * teamMemberInfo.setForeground(Color.white); // Finally we add the
		 * JLabel to the JPanel. leaguePanel.add(teamMemberInfo);
		 */
		String leagueData[][] = new String[L.GetLeagueSize() + 1][5];
		leagueData[0][0] = "    Rank";
		leagueData[0][2] = "Total Points";
		leagueData[0][3] = "Streak";
		leagueData[0][4] = "Moves";

		// This loop is going to get the top 5 teams and output their
		// information to the screen.
		for (int k = 1; k <= L.GetLeagueSize(); k++) {
			Team newTeam = L.GetMember(k);
			
			leagueData[k][0] = "      " + Integer.toString(k);
			leagueData[k][1] = newTeam.GetTeamName() + " (" + newTeam.GetRecord() + ")";
			leagueData[k][2] = Integer.toString(newTeam.CalculateTotalPoints());
			leagueData[k][3] = newTeam.GetStreak();
			leagueData[k][4] = Integer.toString(newTeam.CalculateTotalMoves());

			/*
			 * //Create a blank JLabel that contains a newline character to
			 * space out the team infos JLabel blank = new JLabel("\n");
			 * //Output the information in String format, -> change the font
			 * sizes of both the blank label and the info label JLabel i = new
			 * JLabel("       " + (k) + "        " + newTeam.GetTeamName() +
			 * "  (" + newTeam.GetRecord() +")                     " + +
			 * newTeam.CalculateTotalPoints() + "                    " +
			 * newTeam.GetStreak() + "        " +
			 * newTeam.CalculateTotalMoves()); Font newI = new
			 * Font(i.getFont().getName
			 * (),i.getFont().getStyle(),(int)leaguePanel.getWidth()/2/20); Font
			 * newB = new
			 * Font(blank.getFont().getName(),blank.getFont().getStyle
			 * (),(int)leaguePanel.getWidth()/2/15); i.setFont(newI);
			 * i.setForeground(Color.white); blank.setFont(newB); //Add the
			 * labels to the panel leaguePanel.add(blank); leaguePanel.add(i);
			 */
		}
		String colarr[] = new String[5];
		DefaultTableModel mol = new DefaultTableModel(leagueData, colarr);
		JTable leagueTable = new JTable(mol);
		leagueTable.setBackground(Color.darkGray);
		leagueTable.setForeground(Color.WHITE);
		leagueTable.setRowHeight(32);
		leagueTable.getColumnModel().getColumn(0).setPreferredWidth(50);
		leagueTable.getColumnModel().getColumn(1).setPreferredWidth(160);
		leagueTable.setShowGrid(false);
		// Font newF = new Font("leagueTable",
		// Font.BOLD,(int)leaguePanel.getWidth());

		leaguePanel.add(leagueTable);

		// Second blue panel
		JPanel teamPanel = new JPanel();
		teamPanel.setBackground(Color.blue);
		// Create a new vertical box layout, that way when we add the top five,
		// it will go top to bottom
		teamPanel.setLayout(new BoxLayout(teamPanel, BoxLayout.Y_AXIS));
		// Set the box to the top-left corner of the screen, then top and then
		// left
		teamPanel.setBounds(screenWidth / 2, screenHeight / 3, screenWidth / 2,
				screenHeight / 2);
		teamPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		teamPanel.setAlignmentY(Component.TOP_ALIGNMENT);

		
		MatchUp match = matchup[0];
		Team A = match.getTeam('A');
		Iterator<TeamMem> ateam = A.GetTeam();
		JTable table = new JTable();

		int row = 0;

		while (ateam.hasNext()) {
			TeamMem player = ateam.next();
			String position = player.GetPosition();
			String name = player.GetLastName();
			int count = 0;
			int column = 0;
			data[row][column] = position;
			column++;
			data[row][column] = name;
			column++;
			ArrayList<String> stats = player.statisticsForSeason();
			while (count < stats.size()) {

				data[row][column] = stats.get(count);
				column++;
				count++;
			}

			row ++;
		}
		DefaultTableModel model = new DefaultTableModel(data, col);

		table = new JTable(model);
		table.setBackground(Color.BLACK);
		table.setForeground(Color.WHITE);
		table.setShowGrid(false);
		table.setRowHeight(50);
		table.getColumnModel().getColumn(1).setPreferredWidth(200);
		teamPanel.add(table);

		MouseListener recentListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent event) {
				leagueFrame.setVisible(false); // figure out the source
				MainScreen ms = new MainScreen();
				ms.runMainScreen(league, matchUPS, recentScores); // display the
																	// text in
																	// the
																	// clicked
																	// label
				// message is a JLabel
			}
		};
		leaguePanel.addMouseListener(recentListener);
		teamPanel.addMouseListener(recentListener);

		leagueTable.addMouseListener(recentListener);
		table.addMouseListener(recentListener);

		bigPanel.addMouseListener(recentListener);
		bigPanel.add(leaguePanel);
		bigPanel.add(teamPanel);
		leagueFrame.add(bigPanel);

		// Pack it all up
		leagueFrame.pack();

		// Set the default close operation to eit when you click the X button
		leagueFrame.setDefaultCloseOperation(leagueFrame.EXIT_ON_CLOSE);
		// Center the JFrame
		leagueFrame.setLocationRelativeTo(null);
		// Set the window to full screen mode
		leagueFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
		// Set the visibility
		leagueFrame.setVisible(true);
	}

	/*
	 * public static void display(Object x [][]){ for (int
	 * row=0;row<x.length;row++ ){ for(int
	 * column=0;column<x[row].length;column++){
	 * System.out.print(x[row][column]+"\t"); } System.out.println(); } }
	 */
	public void SetData(String obj, int row_index, int col_index) {
		table.getModel().setValueAt(obj, row_index, col_index);
		// System.out.println("Value is added");
	}

	public void getTeamInfo(Team team) {

	}

	public String[][] populateArray(String[][] x, ArrayList<String> y) {

		int row = 0;
		int column = 0;
		int count = 0;
		String temp;
		while ((row != 10)) {

			// put first 11 items in first row
			temp = y.get(count);

			if (column == 11) {
				column = 0;
				row++;
				x[row][column] = temp;

			} else {
				x[row][column] = temp;
				column++;
			}

		}
		return x;
	}
}